<?php

namespace App\Jobs\goods;

use App\Models\goods\Goods;
use App\Models\goods\GoodsView;
use App\Models\log\ErrorLog;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\DB;

class Views implements ShouldQueue
{


    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 任务最大尝试次数。
     *
     * @var int
     */
    public $tries = 5;

    protected $goods;


    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct( $goods)
    {
//        $this->goods = $goods;
    }

    /**
     *
     * 产品浏览次数叠加
     *
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try{
            $map['goods_id'] = $this->goods->id;

            $view = GoodsView::firstOrNew($map);

            $view->views += 1;
            $view->cate_id = $this->goods->cate_id;
            $view->cate1_id = $this->goods->cate1_id;
            $view->type = $this->goods->type;
            $view->save();

        }catch (\Exception $e){
            dump($e->getMessage());
            $log['msg']     = $e->getMessage();
            $log['params']  = $this->user;
            $log['content'] = $e;
            ErrorLog::create($log);
        }
    }

    public function failed(\Exception $exception)
    {
        dump($exception->getMessage());
        $log['msg']     = $exception->getMessage();
        $log['params']  = $this->user;
        $log['content'] = $exception;
        ErrorLog::create($log);
    }
}
